﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Administration.Backup;
using Microsoft.SharePoint.Administration.Health;

namespace SPHealth.SharePoint.HealthRules.SP.Rules.Availability
{
    class WarnFarmBackup : SPHealthAnalysisRule
    {
        #region Locals
        #endregion

        #region Constructor
        /* This rules does not require a constructor */
        #endregion

        #region Fields

        // the category to display this rule under
        public override SPHealthCategory Category
        {
            get { return SPHealthCategory.Availability; }
        }

        // the type or error to flag this rule as
        public override SPHealthCheckErrorLevel ErrorLevel
        {
            get { return SPHealthCheckErrorLevel.Warning; }
        }

        // the Explanation for the rule and what is not working
        public override string Explanation
        {
            get { return "This farm does not appear to have been backed up by via SharePoint backups. It is important that a farm and its content are backedup with both SharePoint backups and SQL backups. See Backing up a farm - http://technet.microsoft.com/en-gb/library/ee428316.aspx"; }
        }

        // how to fix the issue
        public override string Remedy
        {
            get { return "Ensure that regular SharePoint farm backups are taken. See Plan for backup and recovery (SharePoint 2010) - http://technet.microsoft.com/en-gb/library/cc261687.aspx"; }
        }

        // the Summary for the rule
        public override string Summary
        {
            get { return "The farm has not been backed up via SharePoint backups."; }
        }

        public override SPHealthAnalysisRuleAutomaticExecutionParameters AutomaticExecutionParameters
        {
            get
            {
                SPHealthAnalysisRuleAutomaticExecutionParameters retval = new SPHealthAnalysisRuleAutomaticExecutionParameters();
                retval.Schedule = SPHealthCheckSchedule.Daily;
                retval.Scope = SPHealthCheckScope.Any;
                retval.ServiceType = typeof(SPTimerService);
                return retval;
            }
        }
        #endregion

        #region Methods

        // this method is called whenever the rule is executed
        public override SPHealthCheckStatus Check()
        {
            if (!SPFarm.Joined)
            {
                throw new InvalidOperationException();
            }

            // get the famr backup settings
            SPBackupRestoreConfigurationSettings backupSettings = SPBackupRestoreConfigurationSettings.CreateSettings(SPFarm.Local);

            // determine the results
            if (string.IsNullOrEmpty(backupSettings.PreviousBackupLocation))
                return SPHealthCheckStatus.Failed;
            else
                return SPHealthCheckStatus.Passed;
        }

        #endregion

    }
}
